package com.power.ding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.ding.api.IDi_deptFeign;
import com.power.ding.model.Di_dept;
import com.power.ding.model.pageInfo.Di_deptPageInfo;
import com.power.ding.service.Di_deptService;
import com.power.ding.service.Di_userService;
import com.power.ding.service.Di_userdeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   部门信息控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Di_deptController",tags={"部门信息"})
public class Di_deptController implements IDi_deptFeign {

    @Resource
    private Di_deptService di_deptService;

    @Resource
    private Di_userdeptService di_userdeptService;

    @Resource
    private Di_userService di_userService;
    
    @Override
    @ApiOperation("查询部门信息分页列表")
    @SaCheckPermission("di_dept_list")
    public ResponseDTO list(Di_deptPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Di_dept> page = this.di_deptService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Di_dept di_dept : page) {
                di_dept.geneText();
                list.add(di_dept);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看部门信息")
    @SaCheckPermission("di_dept_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Di_dept di_dept  = di_deptService.getById(id);
        if (di_dept != null) {
            di_dept.geneText();
        }
        return ResponseDTO.succData(di_dept);
    }

    @Override
    @ApiOperation("删除部门信息")
    @SaCheckPermission("di_dept_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        di_deptService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改部门信息")
    @SaCheckPermission("di_dept_save")
    public ResponseDTO save(Di_dept di_dept) {
        Assert.notNull(di_dept, "di_dept不能为空");
        di_deptService.saveOrUpdate(di_dept);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除部门信息")
    @SaCheckPermission("di_dept_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        di_deptService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("di_dept_list")
    public void output(HttpServletResponse response, Di_deptPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "部门信息";
        String[] heads = {"序号","删除标识","最后修改时间","父id","父名称","层级","机构名称","组织机构全称","地址","联系电话","邮编","手机","电子邮件","传真","机构网站网址","拼音全拼","拼音首字母","是否为目录","是否独立机构","是否独立机构","是否临时机构","是否临时机构","状态","状态","行业条线","地理区域","单位级别编号","单位级别名称","机构职能","显示顺序","备注","联系人","机构代码","域名","智能柜标识","机构id"};
        String[] fields = {"id","rowstate","lastupdatetime","fid","fidtext","step","orgname","fullname","address","tel","zipcode","mobile","email","fax","website","pinyin","pinyininitial","isdirectory","isunit","isunittext","istemporary","istemporarytext","status","statustext","hytx","dlqy","orglevel","orgleveltext","orgfunction","showorder","remark","contactuser","unitcode","unitdomain","cabinetcode","unitid"};
        Page<Di_dept> page = this.di_deptService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "部门信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","删除标识","最后修改时间","父id","父名称","层级","机构名称","组织机构全称","地址","联系电话","邮编","手机","电子邮件","传真","机构网站网址","拼音全拼","拼音首字母","是否为目录","是否独立机构","是否独立机构","是否临时机构","是否临时机构","状态","状态","行业条线","地理区域","单位级别编号","单位级别名称","机构职能","显示顺序","备注","联系人","机构代码","域名","智能柜标识","机构id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","rowstate","lastupdatetime","fid","fidtext","step","orgname","fullname","address","tel","zipcode","mobile","email","fax","website","pinyin","pinyininitial","isdirectory","isunit","isunittext","istemporary","istemporarytext","status","statustext","hytx","dlqy","orglevel","orgleveltext","orgfunction","showorder","remark","contactuser","unitcode","unitdomain","cabinetcode","unitid"};
        List<Di_dept> list = (List<Di_dept>)ExcelUtil.Import(fields, file, Di_dept.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.di_deptService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
